package com.itheima.controller;

import com.github.pagehelper.Page;
import com.itheima.pojo.CategoryMenu;
import com.itheima.pojo.User;
import com.itheima.utils.ResultUtil;
import com.itheima.vo.CategoryMenuVO;
import com.itheima.vo.UserVO;
import com.itheima.service.UserService;
import com.itheima.utils.MessageConstant;
import com.itheima.utils.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/user")
@Api(tags = "用户相关接口")
public class UserController {

    @Autowired
    UserService userService;

    /**
     * 删除用户
     * @param userVO
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    @ApiOperation("用户删除")
    public String removeUser(@RequestBody UserVO userVO){
        try {
            int flag = userService.removeUser(userVO.getUid());
            if (flag>0){
                return ResultUtils.result2(1, HttpServletResponse.SC_OK, MessageConstant.USER_DELETE_SUCCESS);
            }else {
                return ResultUtils.result2(0, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageConstant.USER_DELETE_FAIL);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.result2(0, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageConstant.USER_DELETE_FAIL);
        }
    }

    /**
     * 编辑用户信息
     * @param userVo
     * @return
     */
    @PostMapping("/edit")
    @ResponseBody
    @ApiOperation("编辑用户")
    public String editUser(@RequestBody UserVO userVo, HttpServletRequest request){
        User user=new User();
        user.setUserName(userVo.getUserName());
        user.setPassWord(userVo.getPassWord());
        user.setGender(Integer.valueOf(userVo.getGender()));
        user.setAvatar(userVo.getAvatar());
        user.setEmail(userVo.getEmail());
        user.setBirthday(userVo.getBirthday().toString());
        user.setMobile(userVo.getMobile());
        //valid_code无
        user.setSummary(userVo.getSummary());
        //login_count无
        //last_login_time无
        //last_login_ip无
        user.setStatus(userVo.getStatus());
        //create_time无
        user.setUpdateTime( new Timestamp(System.currentTimeMillis()).toString());
        user.setNickName(userVo.getNickName());
        user.setSource(userVo.getSource());
        user.setUuid(userVo.getUuid());
        user.setQqNumber(userVo.getQqNumber());
        user.setWeChat(userVo.getWeChat());
        user.setOccupation(userVo.getOccupation());
        user.setCommentStatus(userVo.getCommentStatus());
        //ip_source无
        //browser无
        //os无
        user.setStartEmailNotification(user.getStartEmailNotification());
        user.setUserTag(userVo.getUserTag());


        try {
            int flag = userService.updateUser(user);
            //判断编辑用户是否成功，返回0表示失败
            if (flag>0){
                return ResultUtils.result2(1, HttpServletResponse.SC_OK, MessageConstant.USER_EDIT_SUCCESS);
            }else {
                return ResultUtils.result2(0, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageConstant.USER_EDIT_FAIL);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.result2(0, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageConstant.USER_EDIT_FAIL);
        }
    }

    @PostMapping("/getList")
    @ResponseBody
    public String getList(@RequestBody(required = false) UserVO userVO) {
        Page<User> logList = userService.getList(userVO);
        Map<String, Object> map = new HashMap();
        map.put("records",logList.getResult());
        map.put("total",logList.getTotal());
        map.put("size",logList.getPageSize());
        map.put("current",logList.getPageNum());
        map.put("orders","");
        map.put("optimizeCountSql",true);
        map.put("isSearchCount",true);
        return ResultUtil.result("success", map);
    }

    @PostMapping("/resetUserPassword")
    @ResponseBody
    @ApiOperation("修改用户密码")
    public String resetUserPassword(@RequestBody UserVO userVO){
        try {
            int flag=userService.resetUserPassword(userVO.getUid(),userVO.getPassWord());
            return ResultUtils.result2(1,HttpServletResponse.SC_OK,MessageConstant.USER_RESET_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.result2(0,HttpServletResponse.SC_INTERNAL_SERVER_ERROR,MessageConstant.USER_RESET_FAIL);
        }
    }
}
